package com.dynamic.mybatis.core.session;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dynamic.mybatis.core.intercept.AbstractDynamicSqlSessionHandler;
import com.dynamic.mybatis.core.intercept.DynamicSqlSessionHandler;
import com.dynamic.mybatis.core.metadata.DynamicMappedStatement;
import com.dynamic.mybatis.core.toolkit.StringUtils;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public abstract class DynamicSqlSessionInterceptHandler extends  DynamicSqlSessionDataSource{
    Set<DynamicSqlSessionHandler> dynamicSqlSessionHandlers = new HashSet<>();



    /**
     　* @Description: 添加全局执行拦截器
     　*/
    public void addSqlSessionHandler(Collection<DynamicSqlSessionHandler> sqlSessionHandlers){
        for(DynamicSqlSessionHandler intercept : sqlSessionHandlers){
            addSqlSessionHandler(intercept);
        }
    }





    public Set<DynamicSqlSessionHandler> sqlSessionHandlers(){
        return dynamicSqlSessionHandlers;
    }

   /**
     　* @Description: 添加执行拦截器
     　* @param namespace 如果为空则为全局拦截器
     　*/
    public void addSqlSessionHandler(DynamicSqlSessionHandler sqlSessionHandler){
        if(sqlSessionHandler == null){
            return;
        }
        this.dynamicSqlSessionHandlers.add(sqlSessionHandler);
    }
    //执行前拦截
    public Object beforeHandle(DynamicMappedStatement dms,Object parameter){
        if(CollectionUtils.isEmpty(this.dynamicSqlSessionHandlers)){ return  parameter ;}

        for(DynamicSqlSessionHandler intercept : this.dynamicSqlSessionHandlers) {
            AbstractDynamicSqlSessionHandler sqlSessionHandler;
            if (intercept instanceof AbstractDynamicSqlSessionHandler && (sqlSessionHandler = (AbstractDynamicSqlSessionHandler) intercept) != null
                && StringUtils.isNotBlank(sqlSessionHandler.getNamespace()) && Objects.equals(sqlSessionHandler.getNamespace(), dms.getNamespace())
                && CollectionUtils.isNotEmpty(sqlSessionHandler.getMappedStatementIds()) && sqlSessionHandler.getMappedStatementIds().contains(dms.getId())
            ) {
                parameter = intercept.before(dms, parameter);
            } else {
                parameter = intercept.before(dms, parameter);
            }
        }
        return parameter;
    }


    //执行前拦截
    public Object afterHandle(DynamicMappedStatement dms,Object parameter,Object result){

        if(CollectionUtils.isEmpty(this.dynamicSqlSessionHandlers)){ return  result ;}

        for(DynamicSqlSessionHandler intercept : this.dynamicSqlSessionHandlers) {
            AbstractDynamicSqlSessionHandler abstractIntercept;
            if (intercept instanceof AbstractDynamicSqlSessionHandler && (abstractIntercept = (AbstractDynamicSqlSessionHandler) intercept) != null
                && StringUtils.isNotBlank(abstractIntercept.getNamespace()) && Objects.equals(abstractIntercept.getNamespace(), dms.getNamespace())
                && CollectionUtils.isNotEmpty(abstractIntercept.getMappedStatementIds()) && abstractIntercept.getMappedStatementIds().contains(dms.getId())
            ) {
                result = intercept.after(dms, parameter,result);
            } else {
                result = intercept.after(dms, parameter,result);
            }
        }
        return result;
    }
}
